package com.lianxian.xindong.commonres.weight;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.github.chrisbanes.photoview.PhotoView;
import com.hxb.library.base.BaseFragment;
import com.hxb.library.di.component.AppComponent;
import com.lianxian.xindong.commonres.R;
import com.lianxian.xindong.commonres.activity.FullImageActivity;

public class ImgeFragment extends BaseFragment {

    private PhotoView fullImage;
    private FullImageActivity fullImageActivity;
    private String imgUrl;
    private View view;
    private ProgressBar progress_bar;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        fullImageActivity = (FullImageActivity) context;
    }


    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {

    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_img, container, false);
        return view;
    }


    @Override
    public void initData(@Nullable Bundle savedInstanceState) {


        imgUrl = getArguments().getString("imgUrl");
        fullImage = (PhotoView)view.findViewById(R.id.full_image);
        progress_bar = (ProgressBar) view.findViewById(R.id.progress_bar);
        progress_bar.setVisibility(View.VISIBLE);
        //Glide.with(getContext()).load(imgUrl).into(fullImage);
        Glide.with(getContext()).asBitmap().load(imgUrl)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        progress_bar.setVisibility(View.GONE);
                        Bitmap bitmap = replaceBitmapColor(resource, Color.TRANSPARENT, Color.WHITE);
                        fullImage.setImageBitmap(bitmap);
                    }
                });

//        fullImage.setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                showDialog(imgUrl);
//                return true;
//            }
//        });

        fullImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //fullImageActivity.overridePendingTransition(0, 0);
                fullImageActivity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                fullImageActivity.finish();

            }
        });

    }


    @Override
    public void setData(@Nullable Object data) {

    }

    /**
     * 建议只能是纯色png图片，不然会造成图片像素或显示极度模糊
     * 将bitmap中的某种颜色值替换成新的颜色
     * @param oldBitmap
     * @param oldColor
     * @param newColor
     * @return
     */
    public Bitmap replaceBitmapColor(Bitmap oldBitmap, int oldColor, int newColor) {
        //相关说明可参考 http://xys289187120.blog.51cto.com/3361352/657590/
        Bitmap mBitmap = oldBitmap.copy(Bitmap.Config.ARGB_8888, true);
        //循环获得bitmap所有像素点
        int mBitmapWidth = mBitmap.getWidth();
        int mBitmapHeight = mBitmap.getHeight();
      /*  G.look("mBitmapWidth: "+mBitmapWidth);
        G.look("mBitmapHeight: "+mBitmapHeight);*/
        for (int i = 0; i < mBitmapHeight; i++) {
            for (int j = 0; j < mBitmapWidth; j++) {
                //获得Bitmap 图片中每一个点的color颜色值
                //将需要填充的颜色值如果不是
                //在这说明一下 如果color 是全透明 或者全黑 返回值为 0
                //getPixel()不带透明通道 getPixel32()才带透明部分 所以全透明是0x00000000
                //而不透明黑色是0xFF000000 如果不计算透明部分就都是0了
                int color = mBitmap.getPixel(j, i);
                //将颜色值存在一个数组中 方便后面修改
                if (color == oldColor) {
                    mBitmap.setPixel(j, i, newColor);  //将白色替换成透明色
                }
            }
        }
        return mBitmap;
    }


}
